package com.ebt.m.customer.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.CustomerUtil;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.model.CustomerModelNew;
import com.ebt.m.customer.ui.ActivityCustomerAll;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

/**
 * Created by Rick.Wang on 2016/4/12.
 */
public class CustomerItemSearchView extends FrameLayout implements ICustomizedViewNew {

    private TextView mTitleView;
    private TextView mNameView;
    private ImageView mAvatarView;
    private CustomerModelNew mCustomerModelNew;

    public CustomerItemSearchView(Context context) {
        this(context, null);
    }

    public CustomerItemSearchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerItemSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = inflate(context, R.layout.view_customer_item_search, this);
        mTitleView = (TextView) contentView.findViewById(R.id.tv_title);
        mAvatarView = (ImageView) contentView.findViewById(R.id.iv_avatar);
        mNameView = (TextView) contentView.findViewById(R.id.tv_name);
        RxView.clicks(contentView.findViewById(R.id.rl_item_view))
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> {
                    Intent intent = new Intent(getContext(), ActivityCustomerAll.class);
                    intent.putExtra(CustomerConstant.CUSTOMER_DETAIL_MODE, CustomerConstant.CUSTOMER_DETAIL_MODE_EDIT);
                    intent.putExtra(CustomerConstant.CUSTOMER_UUID, mCustomerModelNew.getCustomerUuid());
                    intent.putExtra(CustomerConstant.CustomerAll.VIEW_PAGER_INDEX, 0);

                    getContext().startActivity(intent);
                    ((Activity) getContext()).overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                });
    }


    @Override
    public View getView() {
        return this;
    }

    @Override
    public void update(CustomerModelNew model) {
        mCustomerModelNew = model;
        if (model == null) {
            return;
        }
        mNameView.setText(model.name);
        CustomerUtil.setAvatarByUrlAndSex(getContext(), mAvatarView, model.avatarUrl, model.sex);
    }

    @Override
    public void showSortTitle(String sortTitle) {
        if (TextUtils.isEmpty(sortTitle)) {
            mTitleView.setVisibility(View.GONE);
        } else {
            mTitleView.setVisibility(View.VISIBLE);
            mTitleView.setText(sortTitle);
        }
    }
}